﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LibMS.Models;
using Microsoft.Reporting.WebForms;
using System.IO;
using Dapper;

namespace LibMS.Controllers
{
    [Authorize(Roles = "System Admin, Lib Admin")]
    public class ReportController : Controller
    {
        private readonly UnitOfWork _unitOfWork = new UnitOfWork();

        [HttpGet]
        public ActionResult RegBorrowDocument(string bdId)
        {
            string message;
            var parameters = new DynamicParameters();
            parameters.Add("@BorrowDocumentId", bdId);
            var entity = _unitOfWork.LibBorrowDocument.GetById(out message, parameters);
            if (entity == null)//Query error
            {
                return View("ERROR:" + message);
            }

            var localReport = new LocalReport();
            var tempPath = Path.Combine(Server.MapPath("~/Reports"), "BorrowDocuments.rdlc");
             localReport.ReportPath = tempPath;
            localReport.DataSources.Add(new ReportDataSource());

            var regId = new ReportParameter("RegId", entity.BorrowDocumentId.ToString());
            var documentName = new ReportParameter("DocumentName", entity.DocumentName);
            var documentId = new ReportParameter("DocumentId", entity.DocumentId.ToString());
            var documentKyHieu = new ReportParameter("DocumentKyHieu", entity.KyHieu);
            var borrowUserName = new ReportParameter("BorrowUserName", entity.UserName);
            var regDate = new ReportParameter("RegDate", entity.BorrowDate.ToString("dd/MM/yyyy"));
            var borrowTotal = new ReportParameter("BorrowTotal", entity.Total.ToString());
            var returnDate = new ReportParameter("ReturnDate", entity.BorrowDate.AddDays(entity.BorrowDayCount).ToString("dd/MM/yyyy"));
            var borrowReason = new ReportParameter("BorrowReason", (entity.Reason != null && !string.IsNullOrEmpty(entity.Reason)) ? entity.Reason : string.Empty);

            localReport.SetParameters(regId);
            localReport.SetParameters(documentName);
            localReport.SetParameters(documentId);
            localReport.SetParameters(documentKyHieu);
            localReport.SetParameters(borrowUserName);
            localReport.SetParameters(regDate);
            localReport.SetParameters(borrowTotal);
            localReport.SetParameters(returnDate);
            localReport.SetParameters(borrowReason);           

            string reportType = "PDF";
            string mimeType;
            string encoding;
            string fileNameExtension;
            Warning[] warnings;
            string[] streams;
            byte[] renderedBytes;
            renderedBytes = localReport.Render(
                reportType,
                null,
                out mimeType,
                out encoding,
                out fileNameExtension,
                out streams,
                out warnings);

            //Response.AppendHeader("Content-Disposition", "inline; filename=Phieu muon tai lieu.pdf");
            return File(renderedBytes, mimeType, "Phieu muon tai lieu_" + bdId + ".pdf");
        }

    }
}
